#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<jsoncpp/json/json.h>


#define Myself 1

//封装报头
string Encode(string& content)
{
    string package = to_string(content.size());
    package += "\n";
    package += content;
    package += "\n";

    
    return package;

}

//解开报头
bool Decode(string& package, string* content)
{
    int pos = package.find('\n', 0); //查找第一个\n
    if (pos == string::npos) return false;
    
    string len_str = package.substr(0, pos);
    int len = stoi(len_str.c_str());
    ssize_t size = len_str.size() + len + 2;
    if (package.size() < size) return false;

    *content = package.substr(pos + 1, len);
    package.erase(0, size);
    return true;
}


class Request
{
public:
    Request(){}
    Request(int data1, int data2, char oper) : x_(data1), y_(data2), op_(oper){}
    ~Request(){}
    bool Serialize(string* out)
    {
#ifdef Myself

        string s;
        s += to_string(x_);
        s += " ";   
        s += op_;
        s += " ";
        s += to_string(y_);
        
        *out = s;
        return true;
#else
        Json::Value root;
        root["x"] = x_;
        root["y"] = y_;
        root["op"] = op_;

        Json::FastWriter w;
        *out = w.write(root);
        return true;    
#endif

    }

    bool DeSerialize(string in)
    {
#ifdef Myself
        string s = in;
        
        int left = s.find(' ', 0);
        if (left == string::npos) return false;
        string part_x = s.substr(0, left);

        int right = s.find(' ', left + 1);
        if (right == string::npos) return false;
        char part_op = s[left + 1];

        string part_y = s.substr(right + 1);
        x_ = stoi(part_x);
        y_ = stoi(part_y);
        op_ = part_op;
        return true;
#else
    Json::Value root;
    Json::Reader r;
    r.parse(in, root);
    x_ = root["x"];
    y_ = root["y"];
    op_ = root["op"];
#endif
    }

    void DebugPrint()
    {
        cout << "x = " << x_;
        cout << "; lop = " << op_;
        cout << "; y = " << y_ << endl; 
    }
    

    int x_;
    int y_;
    char op_;
};



class Response
{
public:
    Response(){}
    Response(int result, int code) : result_(result), code_(code)
    {}
    ~Response(){}
    bool Serialize(string* out)
    {
#ifdef Myself
        string s;
        s += to_string(result_);
        s += " ";
        s += to_string(code_);
        *out = s;

        return true;

#else
        Json::Value root;
        root["result"] = result_;
        root["code"] = code_;

        Json::FastWriter w;
        *out = w.write(root);
        return true;    
#endif

    }
    
    bool DeSerialize(string in)
    {
#ifdef Myself
        string s = in;

        int pos = s.find(' ', 0);
        if (pos == string::npos) return false;

        string part_res = s.substr(0, pos);
        string part_code = s.substr(pos + 1);
        
        result_ = stoi(part_res);
        code_ = stoi(part_code);


        return true;
#else
    Json::Value root;
    Json::Reader r;
    r.parse(in, root);
    result_ = root["result"];
    code_ = root["code"];
#endif
    }
    
    void DebugPrint()
    {
        cout << "结果响应完成， result: " << result_ << "  , code: " << code_ << endl;
    }


    int result_;
    int code_;
};

